﻿Imports System.Linq
Imports MNL.Store

Partial Class _Default
    Inherits System.Web.UI.Page

    Private dc As MNLDataContext

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        BindPartGrid()

    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender

        BindOrderedPartsGrd()
    End Sub

    Private Sub BindOrderedPartsGrd()

        Dim OrderedParts = CurrentUserShoppingCart.GetItems()
        If OrderedParts IsNot Nothing AndAlso OrderedParts.Count > 0 Then

            lblSubtotal.Text = FormatPrice(Profile.ShoppingCart.Total)

            If Session("shippingCost") IsNot Nothing Then

                lblShippingCost.Text = Session("shippingCost").ToString
                lblTotal.Text = FormatPrice(Profile.ShoppingCart.Total + Decimal.Parse(Session("shippingCost")))

            End If

        Else
            panTotals.Visible = False
        End If

        gvwOrderedParts.DataSource = OrderedParts
        gvwOrderedParts.DataBind()


    End Sub

    Protected Function FormatPrice(ByVal price As Object) As String
        Return Convert.ToDecimal(price).ToString("N2") + " " + "£"
    End Function

    Protected Sub DatalistParts_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DatalistParts.ItemDataBound

        dc = New MNLDataContext

        'If e.Item.ItemType = DataControlCellType.DataCell Then
        Dim PartID As Integer = DatalistParts.DataKeys(e.Item.ItemIndex)


        Dim lblNoOfUsersWhoRated As Label = e.Item.FindControl("lblTotlaNoOfUsersWhoRated")
        Dim lblAvgRatings As Label = e.Item.FindControl("lblAvgRatings")
        Dim lblNoLikes As Label = e.Item.FindControl("lblNoLikes")

        Dim thePart = dc.Parts.Where(Function(t) t.PartID = PartID).First

        If thePart.NoOfRatedCustomers IsNot Nothing Then
            lblNoOfUsersWhoRated.Text = "Avg rating by " & thePart.NoOfRatedCustomers.ToString & " user(s) is "

            '  lblNoOfUsersWhoRated.Text &= thePart.NoOfRatedCustomers

            lblAvgRatings.Text = (thePart.TotalRating / Decimal.Parse(thePart.NoOfRatedCustomers))

        End If

        lblNoLikes.Text = If(thePart.TotalLikes IsNot Nothing AndAlso thePart.TotalLikes > 0, thePart.TotalLikes.ToString, "0")

        'End If
    End Sub

    Protected Sub ddlCategory_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCategory.SelectedIndexChanged
        BindPartGrid()
    End Sub

    Private Sub BindPartGrid()
        dc = New MNLDataContext
        Dim catID = ddlCategory.SelectedValue

        Dim query As IQueryable(Of Part)

        If catID IsNot Nothing AndAlso catID <> 0 Then
            query = dc.Parts.Where(Function(t) t.CategoryID = ddlCategory.SelectedValue)
        Else
            query = dc.Parts
        End If

        DatalistParts.DataSource = query
        DatalistParts.DataBind()

    End Sub
End Class
